package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import java.util.List;
@CrossOrigin
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("findAll")
    public List<User> findAll(){
        return userService.findAll();
    }
    /*
    * 业务:根据id查询数据库
    * 请求类型:get请求
    * 请求路径:http://localhost:8090/getUserById?id=1
    * 请求参数: id=1
    * 返回值结果: User对象
    * MVC:接收参数名称,必须与传递参数的名称一致*/
    @GetMapping ("getUserById")
    public User getUserById(Integer id){
        return userService.getUserById(id);
    }
    @GetMapping("getUserByNS")
    public List<User> getUserByNS(User user){
        return userService.getUserByNS(user);
    }


    @GetMapping("/user/{name}")
    public List<User> getUserByName(@PathVariable String name){
        return userService.getUserByName(name);
    }

    @GetMapping("/user/{name}/{sex}")
    public List<User> getUserBySex(User user){
            return userService.getUserBySex(user);
    }
    @DeleteMapping("/deleteUserByName")
    public String deleteUserByName(User user){
        userService.deleteUserByName(user);
        return  "删除成功";
    }
    @DeleteMapping("/user/{name}/{sex}")
    public String deleteUser(User user){
        userService.deleteUserByName(user);
        return "删除成功";
    }
    /*需求:完成用户的入库操作
    url:http://localhost:8090/saveUser
    参数:对象的jason串{key:value}
    常规数据: id=100 name="tomcat"
    类型: post
    返回值类型: String
    难点:如何将jason串转化为User对象
    springMVC:针对于json转化为对象 开发2个注解
    1.ResponseBody 将对象转化为json串
    2.RequestBody 将json串转化为User对象
    * */
    @PostMapping("/saveUser")
    public String saveUser(@RequestBody User user){
        System.out.println(user);
        userService.saveUser(user);
        return "新增数据成功";
    }

    @PostMapping("/user/{name}/{age}/{sex}")
    public String saveUser2(User user){
        userService.saveUser(user);
        return "新增用户成功";
    }
    @DeleteMapping("/user/{id}")
    public String deleteUserById(User user){
    userService.deleteUserById(user);
    return "删除成功";
    }
    @PutMapping("/updateUser")
    public String updateUser(@RequestBody User user){
        userService.updateUser(user);
        return "修改成功";
    }
}
